package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.SellerInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/17.
 */
public class SellerAdapter extends BaseAdapter {
    private Context mContext;
    private List<SellerInfo> mList = new ArrayList<SellerInfo>();

    public SellerAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<SellerInfo> list){
        if(mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        if(mList != null){
            mList.clear();
        }
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        if(mList != null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag()== null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.seller_item,null);
            childHolder.sellerlogo = (ImageView) convertView.findViewById(R.id.sellerlogo);
            childHolder.sellerName = (TextView) convertView.findViewById(R.id.sellername);
            childHolder.sellerType = (TextView) convertView.findViewById(R.id.sellertype);
            childHolder.sellerRenjun = (TextView) convertView.findViewById(R.id.sellrenjun);

            childHolder.rating= (RatingBar)convertView.findViewById(R.id.ratingbar_Small);

            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

        SellerInfo info = mList.get(position);
        if(info != null){
            String anchorImgUrl = info.getBusinessLogo();
            if(anchorImgUrl != null && anchorImgUrl.length()>0){
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.sellerlogo,anchorImgUrl, null);
            }
            childHolder.sellerName.setText(info.getBusinessName());
            childHolder.sellerType.setText(info.getBusinessType());
            childHolder.sellerRenjun.setText("人均￥"+info.getAverageSpend());
            childHolder.rating.setRating(Float.parseFloat(info.getBusinessScore()));

        }



        return convertView;
    }


    class ChildHolder{
        ImageView sellerlogo;
        TextView sellerName;
        TextView sellerType;
        TextView sellerRenjun;
        RatingBar rating;

    }
}
